import type { Category } from '@/components/CategorySelector/model'
import {defineStore} from 'pinia'
import {
    getCategory1ListApi,
    getCategory2ListApi,
    getCategory3ListApi,
  } from "@/api/product/category";
export const useCategoryStore = defineStore('category',{
    state:():Category=>{
        return {
            category1Id: undefined,
            category2Id: undefined,
            category3Id: undefined,
            category1List: [],
            category2List: [],
            category3List: [],
        }
    },
    getters:{},
    actions:{
        async getCategory1List(){
            this.category1List = await getCategory1ListApi()
        },
        async getCategory2List(category1Id:number){
            this.category2List = await getCategory2ListApi(category1Id);
            // 更新一级分类id
            this.category1Id = category1Id;
            this.category2Id = undefined;
            this.category3Id = undefined;
            // 清空三级分类列表
            this.category3List = [];
        },
        async getCategory3List(category2Id:number){
            this.category3List = await getCategory3ListApi(category2Id)
            this.category2Id = category2Id;
            this.category3Id = undefined;
        }
    }
}) 